package com.ruoyi.basebill.task;

import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.service.impl.OmsPurinwarehsbillServiceImpl;
import com.ruoyi.basebill.service.impl.OmsSumpurinwarehsbillServiceImpl;
import com.ruoyi.basebill.task.util.OmsTaskUtil;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Component("OmsSumPurInWarehsBillTask")
public class OmsSumPurInWarehsBillTask {
    //注入自己
    @Autowired
    private OmsSumPurInWarehsBillTask sumPurInWarehsBillTask;

    //单据自动任务工具类
    @Autowired
    private OmsTaskUtil omsTaskUtil;

    //采入单服务层接口
    @Autowired
    private OmsPurinwarehsbillServiceImpl purinwarehsbillService;

    //汇总采入单服务层接口
    @Autowired
    private OmsSumpurinwarehsbillServiceImpl sumpurinwarehsbillService;

    //汇总销出单服务层接口
    @Autowired
    private DismantlingBillTask dismantlingBillTask;


    /**
     * 查出符合条件的所有采入单，汇总成汇总采入单
     */
    @Transactional
    public void summary(){
        dismantlingBillTask.disBill();
        List<OmsPurinwarehsbill> omsPurinwarehsbills = purinwarehsbillService.selectSumBillNumberIsNull();
        if (StringUtils.isEmpty(omsPurinwarehsbills)){
            return;
        }
        //准备一个集合用来存储汇总采入单
        ArrayList<OmsSumpurinwarehsbill> omsSumpurinwarehsbills = new ArrayList<>();
        //准备一个Map，存储汇总采入单和对应的被汇总的采入单id，用于后续进行反写
        HashMap<String, ArrayList<Long>> sumBillNumberAndIds = new HashMap<>();
        sumPurInWarehsBillTask.summaryPurinwarehsbill(omsPurinwarehsbills,omsSumpurinwarehsbills,sumBillNumberAndIds);
        for (OmsSumpurinwarehsbill omsSumpurinwarehsbill : omsSumpurinwarehsbills) {
            //创建一个新的分录，用来存储自己和自己汇总完的数据
            ArrayList<OmsSumpurinwarehsbillentry> omsSumpurinwarehsbillentries = new ArrayList<>();
            ForA:
            for (OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry : omsSumpurinwarehsbill.getOmsSumpurinwarehsbillentryList()) {
                for (int i = 0; i < omsSumpurinwarehsbillentries.size(); i++) {
                    //合并完之后自己的分录数据
                    OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry1 = omsSumpurinwarehsbillentries.get(i);
                    boolean b = judgeSumBillEntry(omsSumpurinwarehsbillentry, omsSumpurinwarehsbillentry1);
                    if (b){
                        summarySumBillEntry(omsSumpurinwarehsbillentry,omsSumpurinwarehsbillentry1);
                        omsSumpurinwarehsbillentries.set(i,omsSumpurinwarehsbillentry1);
                        continue ForA;
                    }
                }
                omsSumpurinwarehsbillentries.add(omsSumpurinwarehsbillentry);
            }
            omsSumpurinwarehsbill.setOmsSumpurinwarehsbillentryList(omsSumpurinwarehsbillentries);
            sumpurinwarehsbillService.insertOmsSumpurinwarehsbill(omsSumpurinwarehsbill);
        }
        for (String s : sumBillNumberAndIds.keySet()) {
            purinwarehsbillService.updateSumBillNumberByIds(s,sumBillNumberAndIds.get(s));
        }

    }

    /**
     *  传入采购入库单集合，获取汇总单集合以及对应的反写字段数据
     * @param omsPurinwarehsbills 采入单集合
     * @param omsSumpurinwarehsbills 汇总采入单集合
     * @param sumBillNumberAndIds 汇总单编码以及对应的原单id
     */
    public void summaryPurinwarehsbill(List<OmsPurinwarehsbill> omsPurinwarehsbills, ArrayList<OmsSumpurinwarehsbill> omsSumpurinwarehsbills, HashMap<String, ArrayList<Long>> sumBillNumberAndIds) {
        ArrayList<String> billNumbers = new ArrayList<>();
        //如果集合不为null且不为空
        //遍历集合，将id存储在hashset中，用于后续进行批量查询。
        HashSet<Long> idSet = new HashSet<Long>();
        HashMap<Long, OmsPurinwarehsbill> purMap = new HashMap<Long, OmsPurinwarehsbill>();
        if(omsPurinwarehsbills!=null&& !omsPurinwarehsbills.isEmpty()) {
            for (int i = 0; i < omsPurinwarehsbills.size(); i++) {
                OmsPurinwarehsbill omsPurinwarehsbill = omsPurinwarehsbills.get(i);
                Long id = omsPurinwarehsbill.getId();
                idSet.add(id);
            }
            //根据id集合进行批量查询
            List<OmsPurinwarehsbill> omsPurinwarehsbillsList = purinwarehsbillService.selectOmsPurinwarehsbillByIds(idSet);
            //循环集合，将数据存储在map中，用于后续快速获取。
            if(omsPurinwarehsbillsList!=null&& !omsPurinwarehsbillsList.isEmpty()) {
                for (int i = 0; i < omsPurinwarehsbillsList.size(); i++) {
                    OmsPurinwarehsbill omsPurinwarehsbill = omsPurinwarehsbillsList.get(i);
                    Long id = omsPurinwarehsbill.getId();
                    purMap.put(id, omsPurinwarehsbill);
                }
            }
        }
        for (int i = 0; i < omsPurinwarehsbills.size(); i++) {
            OmsPurinwarehsbill omsPurinwarehsbill = omsPurinwarehsbills.get(i);
            omsPurinwarehsbill = purMap.get(omsPurinwarehsbill.getId());
            String summaryPurinBill = sumPurInWarehsBillTask.summaryPurinBill(omsPurinwarehsbill, omsSumpurinwarehsbills, billNumbers);
            Long id = omsPurinwarehsbill.getId();
            if (sumBillNumberAndIds.containsKey(summaryPurinBill)){
                ArrayList<Long> tempIds = sumBillNumberAndIds.get(summaryPurinBill);
                tempIds.add(id);
                sumBillNumberAndIds.put(summaryPurinBill,tempIds);
            }else {
                ArrayList<Long> tempIds = new ArrayList<>();
                tempIds.add(id);
                sumBillNumberAndIds.put(summaryPurinBill,tempIds);
                billNumbers.add(summaryPurinBill);
            }
        }
    }
    public String summaryPurinBill(OmsPurinwarehsbill omsPurinwarehsbill, List<OmsSumpurinwarehsbill> omsSumpurinwarehsbills, ArrayList<String> billNumbers) {
        //1.查找匹配的单据
        int matchIndex = -1;
        for (int i = 0; i < omsSumpurinwarehsbills.size(); i++) {
            OmsSumpurinwarehsbill omsSumpurinwarehsbill = omsSumpurinwarehsbills.get(i);
            boolean b = sumPurInWarehsBillTask.judgeBillHead(omsPurinwarehsbill, omsSumpurinwarehsbill);
            if (b){
                matchIndex = i;
                break;
            }
        }

        //2.如果没有匹配的单据，则创建一个新的汇总单据
        if (matchIndex == -1) {
            OmsSumpurinwarehsbill omsSumpurinwarehsbill = new OmsSumpurinwarehsbill();
            sumPurInWarehsBillTask.initializeOneBill(omsPurinwarehsbill, omsSumpurinwarehsbill, billNumbers);
            //为新的分录初始化数据
            ArrayList<OmsSumpurinwarehsbillentry> omsSumpurinwarehsbillentries = new ArrayList<>();
            for (OmsPurinwarehsbillentry omsPurinwarehsbillentry : omsPurinwarehsbill.getOmsPurinwarehsbillentryList()) {
                OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry = new OmsSumpurinwarehsbillentry();
                sumPurInWarehsBillTask.initializeOneBillEntry(omsPurinwarehsbillentry, omsSumpurinwarehsbillentry);
                omsSumpurinwarehsbillentries.add(omsSumpurinwarehsbillentry);
            }
            omsSumpurinwarehsbill.setOmsSumpurinwarehsbillentryList(omsSumpurinwarehsbillentries);
            omsSumpurinwarehsbills.add(omsSumpurinwarehsbill);
            return omsSumpurinwarehsbill.getBillnumber();
        }

        //3.如果有匹配的单据，则将采购入库单添加到对应的汇总单据中
        OmsSumpurinwarehsbill omsSumpurinwarehsbill = omsSumpurinwarehsbills.get(matchIndex);
        List<OmsSumpurinwarehsbillentry> omsSumpurinwarehsbillentryList = omsSumpurinwarehsbill.getOmsSumpurinwarehsbillentryList();

        //创建查找表，方便查找对应的分录
        Map<String, Integer> entryIndexMap = createEntryIndexMap(omsSumpurinwarehsbillentryList);

        //遍历采购入库单的分录，查找对应的分录，如果没有则创建新的分录
        for (OmsPurinwarehsbillentry omsPurinwarehsbillentry : omsPurinwarehsbill.getOmsPurinwarehsbillentryList()) {
            String key = generateEntryKey(omsPurinwarehsbillentry);
            if (entryIndexMap.containsKey(key)) {
                int index = entryIndexMap.get(key);
                OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry1 = omsSumpurinwarehsbillentryList.get(index);
                sumPurInWarehsBillTask.summaryBillEntry(omsPurinwarehsbillentry, omsSumpurinwarehsbillentry1);
                omsSumpurinwarehsbillentryList.set(index, omsSumpurinwarehsbillentry1);
            } else {
                OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry1 = new OmsSumpurinwarehsbillentry();
                sumPurInWarehsBillTask.initializeOneBillEntry(omsPurinwarehsbillentry, omsSumpurinwarehsbillentry1);
                omsSumpurinwarehsbillentryList.add(omsSumpurinwarehsbillentry1);
                //更新索引表
                entryIndexMap.put(key, omsSumpurinwarehsbillentryList.size() - 1);
            }
        }
    return omsSumpurinwarehsbill.getBillnumber();

    }

    private Map<String, Integer> createEntryIndexMap(List<OmsSumpurinwarehsbillentry> entries) {
        Map<String, Integer> indexMap = new HashMap<>();
        for (int i = 0; i < entries.size(); i++) {
            String key = generateEntryKey(entries.get(i));
            indexMap.put(key, i);
        }
        return indexMap;
    }

    private String generateEntryKey(OmsSumpurinwarehsbillentry entry) {
        // 根据您的业务逻辑生成唯一键
        // 这里假设使用某些属性组合作为键，例如：
        String warehousenumber = entry.getWarehousenumber();
        String materialnumber = entry.getMaterialnumber();
        String unitnumber = entry.getUnitnumber();
        String baseunitnumber = entry.getBaseunitnumber();
        BigDecimal price = entry.getPrice();
        BigDecimal taxrate = entry.getTaxrate();
        BigDecimal taxprice = entry.getTaxprice();
        Long ispresent = entry.getIspresent();
        return warehousenumber + ':' + materialnumber + ':' + unitnumber + ':' + baseunitnumber + ':' + price + ':' + taxrate + ':' + taxprice + ':' + ispresent;
        // 注意：请根据实际情况调整这个键的生成逻辑，使用真正能唯一标识条目的属性组合
    }

    private String generateEntryKey(OmsPurinwarehsbillentry entry) {
        // 根据您的业务逻辑生成唯一键
        // 这里假设使用某些属性组合作为键，例如：
        String warehousenumber = entry.getWarehousenumber();
        String materialnumber = entry.getMaterialnumber();
        String unitnumber = entry.getUnitnumber();
        String baseunitnumber = entry.getBaseunitnumber();
        BigDecimal price = entry.getPrice();
        BigDecimal taxrate = entry.getTaxrate();
        BigDecimal taxprice = entry.getTaxprice();
        Long ispresent = entry.getIspresent();
        return warehousenumber + ':' + materialnumber + ':' + unitnumber + ':' + baseunitnumber + ':' + price + ':' + taxrate + ':' + taxprice + ':' + ispresent;

        // 注意：请根据实际情况调整这个键的生成逻辑，使用真正能唯一标识条目的属性组合
    }




        /**
         * 将采入单汇总到汇总采入单
         * @param omsPurinwarehsbill 采入单
         * @param omsSumpurinwarehsbills 汇总采入单
         * @param billNumbers 汇总采入单单据编码集合
         * @return 汇总的汇总采入单编码
         */
    public String summaryPurinBill2(OmsPurinwarehsbill omsPurinwarehsbill, List<OmsSumpurinwarehsbill> omsSumpurinwarehsbills, ArrayList<String> billNumbers) {
        for (int i = 0; i < omsSumpurinwarehsbills.size(); i++) {
            OmsSumpurinwarehsbill omsSumpurinwarehsbill = omsSumpurinwarehsbills.get(i);
            boolean b = sumPurInWarehsBillTask.judgeBillHead(omsPurinwarehsbill, omsSumpurinwarehsbill);
            if (b){
                ForA :
                for (OmsPurinwarehsbillentry omsPurinwarehsbillentry : omsPurinwarehsbill.getOmsPurinwarehsbillentryList()) {
                    List<OmsSumpurinwarehsbillentry> omsSumpurinwarehsbillentryList = omsSumpurinwarehsbill.getOmsSumpurinwarehsbillentryList();
                    for (int j = 0; j < omsSumpurinwarehsbillentryList.size(); j++) {
                        OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry = omsSumpurinwarehsbillentryList.get(j);
                        boolean b1 = sumPurInWarehsBillTask.judgeBillEntry(omsPurinwarehsbillentry, omsSumpurinwarehsbillentry);
                        if (b1){
                            sumPurInWarehsBillTask.summaryBillEntry(omsPurinwarehsbillentry,omsSumpurinwarehsbillentry);
                            omsSumpurinwarehsbillentryList.set(j,omsSumpurinwarehsbillentry);
                            continue ForA;
                        }
                    }
                    OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry = new OmsSumpurinwarehsbillentry();
                    sumPurInWarehsBillTask.initializeOneBillEntry(omsPurinwarehsbillentry,omsSumpurinwarehsbillentry);
                    omsSumpurinwarehsbillentryList.add(omsSumpurinwarehsbillentry);
                    omsSumpurinwarehsbill.setOmsSumpurinwarehsbillentryList(omsSumpurinwarehsbillentryList);
                }
                omsSumpurinwarehsbills.set(i,omsSumpurinwarehsbill);
                return omsSumpurinwarehsbill.getBillnumber();


            }
        }
        OmsSumpurinwarehsbill omsSumpurinwarehsbill = new OmsSumpurinwarehsbill();
        sumPurInWarehsBillTask.initializeOneBill(omsPurinwarehsbill,omsSumpurinwarehsbill,billNumbers);
        omsSumpurinwarehsbills.add(omsSumpurinwarehsbill);
        return omsSumpurinwarehsbill.getBillnumber();
    }


    /**
     * 判断单据头是否可以合并
     * @param omsPurinwarehsbill 采入单
     * @param omsSumpurinwarehsbill 汇总采入单
     * @return 结果
     */
    public boolean judgeBillHead(OmsPurinwarehsbill omsPurinwarehsbill, OmsSumpurinwarehsbill omsSumpurinwarehsbill) {
        Date bizdate = omsPurinwarehsbill.getBizdate();
        String storageorgnumber = omsPurinwarehsbill.getStorageorgnumber();
        String biztypenumber = omsPurinwarehsbill.getBiztypenumber();
        String transactiontypenumber = omsPurinwarehsbill.getTransactiontypenumber();
        String suppliernumber = omsPurinwarehsbill.getSuppliernumber();
        String accountcompanynumber = omsPurinwarehsbill.getAccountcompanynumber();
        String costcenternumber = omsPurinwarehsbill.getCostcenternumber();
        String currency = omsPurinwarehsbill.getCurrency();
        BigDecimal exchangerate = omsPurinwarehsbill.getExchangerate();
        String srcbilltype = omsPurinwarehsbill.getSrcbilltype();
        String srcsys = omsPurinwarehsbill.getSrcsys();

        Date bizdate1 = omsSumpurinwarehsbill.getBizdate();
        String storageorgnumber1 = omsSumpurinwarehsbill.getStorageorgnumber();
        String biztypenumber1 = omsSumpurinwarehsbill.getBiztypenumber();
        String transactiontypenumber1 = omsSumpurinwarehsbill.getTransactiontypenumber();
        String suppliernumber1 = omsSumpurinwarehsbill.getSuppliernumber();
        String accountcompanynumber1 = omsSumpurinwarehsbill.getAccountcompanynumber();
        String costcenternumber1 = omsSumpurinwarehsbill.getCostcenternumber();
        String currency1 = omsSumpurinwarehsbill.getCurrency();
        BigDecimal exchangerate1 = omsSumpurinwarehsbill.getExchangerate();
        String description = omsSumpurinwarehsbill.getDescription();
        String srcsys1 = omsSumpurinwarehsbill.getSrcsys();
        //单据类型
        boolean boolean10 = OmsTaskUtil.juedeString(srcbilltype, description);
        if (!boolean10){
            return false;
        }
        //业务日期
        boolean boolean1 = OmsTaskUtil.juedeDate(bizdate,bizdate1);
        //库存组织编码
        boolean boolean2 = OmsTaskUtil.juedeString(storageorgnumber,storageorgnumber1);
        //业务类型编码
        boolean boolean3 = OmsTaskUtil.juedeString(biztypenumber,biztypenumber1);
        //事务类型编码
        boolean boolean4 = OmsTaskUtil.juedeString(transactiontypenumber,transactiontypenumber1);
        //供应商编码
        boolean boolean5 = OmsTaskUtil.juedeString(suppliernumber,suppliernumber1);
        //记账公司编码
        boolean boolean6 = OmsTaskUtil.juedeString(accountcompanynumber,accountcompanynumber1);
        //成本中心编码
        boolean boolean7 = OmsTaskUtil.juedeString(costcenternumber,costcenternumber1);
        //币别
        boolean boolean8 = OmsTaskUtil.juedeString(currency,currency1);
        //汇率
        boolean boolean9 = OmsTaskUtil.juedeBigDecimal(exchangerate,exchangerate1);
        //来源系统
        boolean boolean11 = OmsTaskUtil.juedeString(srcsys, srcsys1);


        return boolean1&&boolean2&&boolean3&&boolean4&&boolean5&&boolean6&&boolean7&&boolean8&&boolean9&&boolean11;
    }


    /**
     * 判断分录是否可以合并
     * @param omsPurinwarehsbillentry 采入单分录
     * @param omsSumpurinwarehsbillentry 汇总采入单分录
     * @return 结果
     */
    public boolean judgeBillEntry(OmsPurinwarehsbillentry omsPurinwarehsbillentry, OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry) {
        String warehousenumber = omsPurinwarehsbillentry.getWarehousenumber();
        String materialnumber = omsPurinwarehsbillentry.getMaterialnumber();
        String unitnumber = omsPurinwarehsbillentry.getUnitnumber();
        String baseunitnumber = omsPurinwarehsbillentry.getBaseunitnumber();
        BigDecimal price = omsPurinwarehsbillentry.getPrice();
        BigDecimal taxrate = omsPurinwarehsbillentry.getTaxrate();
        BigDecimal taxprice = omsPurinwarehsbillentry.getTaxprice();
        Long ispresent = omsPurinwarehsbillentry.getIspresent();


        String warehousenumber1 = omsSumpurinwarehsbillentry.getWarehousenumber();
        String materialnumber1 = omsSumpurinwarehsbillentry.getMaterialnumber();
        String unitnumber1 = omsSumpurinwarehsbillentry.getUnitnumber();
        String baseunitnumber1 = omsSumpurinwarehsbillentry.getBaseunitnumber();
        BigDecimal price1 = omsSumpurinwarehsbillentry.getPrice();
        BigDecimal taxrate1 = omsSumpurinwarehsbillentry.getTaxrate();
        BigDecimal taxprice1 = omsSumpurinwarehsbillentry.getTaxprice();
        Long ispresent1 = omsSumpurinwarehsbillentry.getIspresent();

        boolean boolean1 = warehousenumber.equals(warehousenumber1);
        boolean boolean2 = materialnumber.equals(materialnumber1);
        boolean boolean3 = unitnumber.equals(unitnumber1);
        boolean boolean4 = baseunitnumber.equals(baseunitnumber1);
        boolean boolean5 = price.equals(price1);
        boolean boolean6 = taxrate.equals(taxrate1);
        boolean boolean7 = taxprice.equals(taxprice1);
        boolean boolean8 = ispresent.equals(ispresent1);

        return boolean1&&boolean2&&boolean3&&boolean4&&boolean5&&boolean6&&boolean7&&boolean8;
    }

    public boolean  judgeSumBillEntry(OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry1, OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry2){
        String warehousenumber1 = omsSumpurinwarehsbillentry1.getWarehousenumber();
        String materialnumber1 = omsSumpurinwarehsbillentry1.getMaterialnumber();
        String unitnumber1 = omsSumpurinwarehsbillentry1.getUnitnumber();
        String baseunitnumber1 = omsSumpurinwarehsbillentry1.getBaseunitnumber();
        BigDecimal price1 = omsSumpurinwarehsbillentry1.getPrice();
        BigDecimal taxrate1 = omsSumpurinwarehsbillentry1.getTaxrate();
        BigDecimal taxprice1 = omsSumpurinwarehsbillentry1.getTaxprice();
        Long ispresent1 = omsSumpurinwarehsbillentry1.getIspresent();


        String warehousenumber2 = omsSumpurinwarehsbillentry2.getWarehousenumber();
        String materialnumber2 = omsSumpurinwarehsbillentry2.getMaterialnumber();
        String unitnumber2 = omsSumpurinwarehsbillentry2.getUnitnumber();
        String baseunitnumber2 = omsSumpurinwarehsbillentry2.getBaseunitnumber();
        BigDecimal price2 = omsSumpurinwarehsbillentry2.getPrice();
        BigDecimal taxrate2 = omsSumpurinwarehsbillentry2.getTaxrate();
        BigDecimal taxprice2 = omsSumpurinwarehsbillentry2.getTaxprice();
        Long ispresent2 = omsSumpurinwarehsbillentry2.getIspresent();

        boolean boolean1 = warehousenumber2.equals(warehousenumber1);
        boolean boolean2 = materialnumber2.equals(materialnumber1);
        boolean boolean3 = unitnumber2.equals(unitnumber1);
        boolean boolean4 = baseunitnumber2.equals(baseunitnumber1);
        boolean boolean5 = price2.equals(price1);
        boolean boolean6 = taxrate2.equals(taxrate1);
        boolean boolean7 = taxprice2.equals(taxprice1);
        boolean boolean8 = ispresent2.equals(ispresent1);

        return boolean1&&boolean2&&boolean3&&boolean4&&boolean5&&boolean6&&boolean7&&boolean8;
    }


    /**
     * 合并采入单和汇总采入单分录数据
     * @param omsPurinwarehsbillentry 采入单分录
     * @param omsSumpurinwarehsbillentry 汇总采入单分录
     */
    public void summaryBillEntry(OmsPurinwarehsbillentry omsPurinwarehsbillentry, OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry) {
        BigDecimal qty = omsPurinwarehsbillentry.getQty();
        BigDecimal baseqty = omsPurinwarehsbillentry.getBaseqty();
        BigDecimal amount = omsPurinwarehsbillentry.getAmount();
        BigDecimal tax = omsPurinwarehsbillentry.getTax();
        BigDecimal taxamount = omsPurinwarehsbillentry.getTaxamount();

        omsSumpurinwarehsbillentry.setQty(omsSumpurinwarehsbillentry.getQty().add(qty));
        omsSumpurinwarehsbillentry.setBaseqty(omsSumpurinwarehsbillentry.getBaseqty().add(baseqty));
        omsSumpurinwarehsbillentry.setAmount(omsSumpurinwarehsbillentry.getAmount().add(amount));
        omsSumpurinwarehsbillentry.setTax(omsSumpurinwarehsbillentry.getTax().add(tax));
        omsSumpurinwarehsbillentry.setTaxamount(omsSumpurinwarehsbillentry.getTaxamount().add(taxamount));
    }

    public void summarySumBillEntry(OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry1, OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry2) {
        BigDecimal qty = omsSumpurinwarehsbillentry1.getQty();
        BigDecimal baseqty = omsSumpurinwarehsbillentry1.getBaseqty();
        BigDecimal amount = omsSumpurinwarehsbillentry1.getAmount();
        BigDecimal tax = omsSumpurinwarehsbillentry1.getTax();
        BigDecimal taxamount = omsSumpurinwarehsbillentry1.getTaxamount();

        omsSumpurinwarehsbillentry2.setQty(omsSumpurinwarehsbillentry2.getQty().add(qty));
        omsSumpurinwarehsbillentry2.setBaseqty(omsSumpurinwarehsbillentry2.getBaseqty().add(baseqty));
        omsSumpurinwarehsbillentry2.setAmount(omsSumpurinwarehsbillentry2.getAmount().add(amount));
        omsSumpurinwarehsbillentry2.setTax(omsSumpurinwarehsbillentry2.getTax().add(tax));
        omsSumpurinwarehsbillentry2.setTaxamount(omsSumpurinwarehsbillentry2.getTaxamount().add(taxamount));
    }





        /**
         * 传入采入单、汇总采入单、汇总单据编号集合来初始化汇总采入单的数据
         * @param omsPurinwarehsbill 采入单
         * @param omsSumpurinwarehsbill 汇总采入单
         * @param billNumbers 汇总采入单编号集合
         */
    public void initializeOneBill(OmsPurinwarehsbill omsPurinwarehsbill, OmsSumpurinwarehsbill omsSumpurinwarehsbill, ArrayList<String> billNumbers) {
        Date bizdate = omsPurinwarehsbill.getBizdate();
        String storageorgnumber = omsPurinwarehsbill.getStorageorgnumber();
        String storageorgname = omsPurinwarehsbill.getStorageorgname();
        String biztypenumber = omsPurinwarehsbill.getBiztypenumber();
        String biztypename = omsPurinwarehsbill.getBiztypename();
        String transactiontypenumber = omsPurinwarehsbill.getTransactiontypenumber();
        String transactiontypename = omsPurinwarehsbill.getTransactiontypename();
        String suppliernumber = omsPurinwarehsbill.getSuppliernumber();
        String suppliername = omsPurinwarehsbill.getSuppliername();
        String costcenternumber = omsPurinwarehsbill.getCostcenternumber();
        String costcentername = omsPurinwarehsbill.getCostcentername();
        String accountcompanynumber = omsPurinwarehsbill.getAccountcompanynumber();
        String accountcompanyname = omsPurinwarehsbill.getAccountcompanyname();
        String currency = omsPurinwarehsbill.getCurrency();
        BigDecimal exchangerate = omsPurinwarehsbill.getExchangerate();
        String srcbilltype = omsPurinwarehsbill.getSrcbilltype();
        //来源系统
        String srcsys = omsPurinwarehsbill.getSrcsys();

        omsSumpurinwarehsbill.setBizdate(bizdate);
        String billNumber = sumPurInWarehsBillTask.getNextBillNumber(omsSumpurinwarehsbill, billNumbers);
        omsSumpurinwarehsbill.setBillnumber(billNumber);
        omsSumpurinwarehsbill.setStorageorgnumber(storageorgnumber);
        omsSumpurinwarehsbill.setStorageorgname(storageorgname);
        omsSumpurinwarehsbill.setBiztypenumber(biztypenumber);
        omsSumpurinwarehsbill.setBiztypename(biztypename);
        omsSumpurinwarehsbill.setTransactiontypenumber(transactiontypenumber);
        omsSumpurinwarehsbill.setTransactiontypename(transactiontypename);
        omsSumpurinwarehsbill.setSuppliernumber(suppliernumber);
        omsSumpurinwarehsbill.setSuppliername(suppliername);
        omsSumpurinwarehsbill.setCostcenternumber(costcenternumber);
        omsSumpurinwarehsbill.setCostcentername(costcentername);
        omsSumpurinwarehsbill.setAccountcompanynumber(accountcompanynumber);
        omsSumpurinwarehsbill.setAccountcompanyname(accountcompanyname);
        omsSumpurinwarehsbill.setCurrency(currency);
        omsSumpurinwarehsbill.setExchangerate(exchangerate);
        omsSumpurinwarehsbill.setDescription(srcbilltype);
        omsSumpurinwarehsbill.setCreatetime(new Date());
        omsSumpurinwarehsbill.setIstrans("0");
        //来源系统
        omsSumpurinwarehsbill.setSrcsys(srcsys);

        List<OmsPurinwarehsbillentry> omsPurinwarehsbillentryList = omsPurinwarehsbill.getOmsPurinwarehsbillentryList();
        ArrayList<OmsSumpurinwarehsbillentry> omsSumpurinwarehsbillentries = new ArrayList<>();
        if (StringUtils.isEmpty(omsPurinwarehsbillentryList)){
            omsSumpurinwarehsbill.setOmsSumpurinwarehsbillentryList(omsSumpurinwarehsbillentries);
            return;
        }
        for (OmsPurinwarehsbillentry omsPurinwarehsbillentry : omsPurinwarehsbillentryList) {
            OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry = new OmsSumpurinwarehsbillentry();
            sumPurInWarehsBillTask.initializeOneBillEntry(omsPurinwarehsbillentry,omsSumpurinwarehsbillentry);
            omsSumpurinwarehsbillentries.add(omsSumpurinwarehsbillentry);
        }
        omsSumpurinwarehsbill.setOmsSumpurinwarehsbillentryList(omsSumpurinwarehsbillentries);

    }

    /**
     * 传入采入单分录、汇总采入单分录进行初始化汇总采入单分录数据
     * @param omsPurinwarehsbillentry 采入单分录
     * @param omsSumpurinwarehsbillentry 汇总采入单分录
     */
    public void initializeOneBillEntry(OmsPurinwarehsbillentry omsPurinwarehsbillentry, OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry) {
        String warehousenumber = omsPurinwarehsbillentry.getWarehousenumber();
        String warehousename = omsPurinwarehsbillentry.getWarehousename();
        String materialnumber = omsPurinwarehsbillentry.getMaterialnumber();
        String materialname = omsPurinwarehsbillentry.getMaterialname();
        String unitnumber = omsPurinwarehsbillentry.getUnitnumber();
        String baseunitnumber = omsPurinwarehsbillentry.getBaseunitnumber();
        BigDecimal qty = omsPurinwarehsbillentry.getQty();
        BigDecimal baseqty = omsPurinwarehsbillentry.getBaseqty();
        BigDecimal price = omsPurinwarehsbillentry.getPrice();
        BigDecimal taxrate = omsPurinwarehsbillentry.getTaxrate();
        BigDecimal taxprice = omsPurinwarehsbillentry.getTaxprice();
        BigDecimal amount = omsPurinwarehsbillentry.getAmount();
        BigDecimal tax = omsPurinwarehsbillentry.getTax();
        BigDecimal taxamount = omsPurinwarehsbillentry.getTaxamount();
        Long ispresent = omsPurinwarehsbillentry.getIspresent();

        omsSumpurinwarehsbillentry.setWarehousenumber(warehousenumber);
        omsSumpurinwarehsbillentry.setWarehousename(warehousename);
        omsSumpurinwarehsbillentry.setMaterialnumber(materialnumber);
        omsSumpurinwarehsbillentry.setMaterialname(materialname);
        omsSumpurinwarehsbillentry.setUnitnumber(unitnumber);
        omsSumpurinwarehsbillentry.setBaseunitnumber(baseunitnumber);
        omsSumpurinwarehsbillentry.setQty(qty);
        omsSumpurinwarehsbillentry.setBaseqty(baseqty);
        omsSumpurinwarehsbillentry.setPrice(price);
        omsSumpurinwarehsbillentry.setTaxrate(taxrate);
        omsSumpurinwarehsbillentry.setTaxprice(taxprice);
        omsSumpurinwarehsbillentry.setAmount(amount);
        omsSumpurinwarehsbillentry.setTax(tax);
        omsSumpurinwarehsbillentry.setTaxamount(taxamount);
        omsSumpurinwarehsbillentry.setIspresent(ispresent);

    }

    /**
     * 判断对应的业务日期的单据是否存在，并生成单据编号
     * @param sumpurinwarehsbill 汇总采入单
     * @param billNumbers 汇总采入单单据编号集合
     * @return 生成的汇总采入单单据编号
     */
    public String getNextBillNumber(OmsSumpurinwarehsbill sumpurinwarehsbill,ArrayList<String> billNumbers){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String billNumberPfix = format.format(sumpurinwarehsbill.getBizdate());
        if (StringUtils.isEmpty(billNumbers)){
            return omsTaskUtil.setBillNumber(sumpurinwarehsbill);
        }else {
            for (int i = billNumbers.size()-1; i >= 0; i--){
                String billNumber = billNumbers.get(i);
                if (billNumber.contains(billNumberPfix)){
                    String billNumberEnd = billNumber.substring(billNumber.length() - 6);
                    int endNumber = Integer.parseInt(billNumberEnd);
                    int tempNumber = 1000000 + endNumber + 1;
                    billNumber = "HZCGRK" + '-' + billNumberPfix + '-' + String.valueOf(tempNumber).substring(1);
                    return billNumber;
                }
            }
            return omsTaskUtil.setBillNumber(sumpurinwarehsbill);
        }
    }

}
